#include <iostream>
#include <algorithm>
using namespace std;

void bubblesort_(pair<int, int> a[], int size)
{
    bool sorted = false;
    for (int i = 0; i < size; i++)
    {
        sorted = false;
        for (int j = 0; j < size - 1 - i; j++)
        {
            if (a[j].second < a[j + 1].second)
            {
                swap(a[j], a[j + 1]);
                sorted = true;
            }
            else if (a[j].second == a[j + 1].second)
            {
                if (a[j].first > a[j + 1].first)
                {
                    swap(a[j], a[j + 1]);
                    sorted = true;
                }
            }
        }
    }
}

int main()
{
    pair<int, int> a[4] = {
        {1006, 98},
        {1002, 97},
        {1002, 99},
        {1006, 99}};

    bubblesort_(a, 4);
    for (int i = 0; i < 4; i++)
    {
        cout << a[i].first << ' ' << a[i].second << endl;
    }
    return 0;
}